package com.open.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * 
 * @author TR
 *
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

	@Autowired
	private DataSource dataSource;
	
	//@Autowired
	//private RedisConnectionFactory redisConnectionFactory;

	@Override
	public void configure(HttpSecurity http) throws Exception {
		// 全部需要认证
		http.authorizeRequests().anyRequest().authenticated();
	}

	@Bean
	public TokenStore getTokenStore() {
		//Redis存储
		//TokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
		//TokenStore jwtTokenStore = JwtTokenStore();
		//数据库存储
		TokenStore jdbcTokenStore = new JdbcTokenStore(dataSource);
		return jdbcTokenStore;
	}
	
	@Override
	public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
		  TokenStore tokenStore = getTokenStore();
		  
	}
}
